<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no"/>
    <title>新的朋友</title>
    <link rel="stylesheet" type="text/css" href="../../css/aui.css" />
    <style type="text/css">
        .aui-img-object{
            width: 30px !important;
            height: 30px !important;
        }
        .aui-list-view p{
            font-size:12px;
        }
    </style>
</head>
<body>
    <div class="aui-content">
        <ul class="aui-list-view" id="list-user" style="display: none;">
        </ul>
    </div>

</body>
<script id="list-template" type="text/x-dot-template">
    {{ for(var i in it){ }}
    {{? it[i].sender }}
    <li class="aui-list-view-cell aui-img">
        <img class="aui-img-object aui-pull-left" src="{{=it[i].sender.thumb}}" onclick="go2User({{=it[i].sender.userid}},'../')">
        <div class="aui-img-body">
            <div class="aui-pull-left">
                <span onclick="go2User({{=it[i].sender.userid}},'../')">{{=it[i].sender.company}}</span>
                <!-- <p>200米以内</p> -->
            </div>
            {{? it[i].relation==2}}
            <div class="aui-pull-right aui-btn aui-btn-default">已添加</div>
            {{?? it[i].relation==1}}
            <div class="aui-pull-right aui-btn aui-btn-default">已发送</div>
            {{??}}
            <div class="aui-pull-right aui-btn aui-btn-primary" onclick="addFriend(this)" data-userid="{{=it[i].sender_userid}}">接受</div>
            {{?}}
        </div>
    </li>
    {{?}}
    {{ } }}
</script>

<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>

<script type="text/javascript" src="../../script/doT.min.js"></script>
<script type="text/javascript" src="../../script/jquery-1.11.3.js"></script>

<script type="text/javascript">
    var isLock=false;
    apiready=function (argument) {
       api.parseTapmode();

        //计数器清零
        clearCount();

       api.setRefreshHeaderInfo({
            visible : true,
            loadingImg : 'widget://image/ptr_pull.png',
            bgColor : '#efeff4',
            textColor : '#959595',
            textDown : '下拉刷新',
            textUp : '松开刷新',
            showTime : true
        }, function(ret, err) {
            isLock=false;
            page = 1;
            loadData();
        });     

        api.addEventListener({
            name: 'refreshNewfriendList'
        }, function(ret, err) {
            isLock=false;
            page = 1;
            api.refreshHeaderLoading();        
        });

        api.refreshHeaderLoading();
    }

    function loadData(){
           api.refreshHeaderLoading();

           getStorageAll();

           var data = {};
           var url='Notification/getList?username='+user_name;

           if(!isLock){
            isLock = true; 
            ajaxRequest(url, 'get', data, function (ret, err) {
                api.refreshHeaderLoadDone();            

                if (ret.status == 1) {
                    var tpl = $api.byId('list-template').text;
                    var tempFn = doT.template(tpl);
                    $('#list-user').show().html(tempFn(ret.msg));
                } else {
                    api.toast({
                        msg: ret.msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                isLock = false;
            });  
        }     
    }


    //单击加为人脉
    function addFriend(obj) {
           getStorageAll();
           var $obj=$(obj);
           var fuserid=$obj.attr('data-userid');
           ajaxRequest('Friendship/follow?username='+user_name, 'post', {fuserid:fuserid}, function (ret, err) {
            if (ret.status == 1&&(typeof ret.msg !== 'string')) {
                $obj.removeClass('aui-btn-primary').addClass('aui-btn-default');
                $obj.text(ret.msg.relation==2?'已添加':'已发送');

                api.sendEvent({
                    name:'refreshContactList'
                });
            } else {
                api.toast({
                    msg: ret.msg,
                    duration: 2000,
                    location: 'bottom'
                })
            }
            })
       }


       function clearCount(){
            api.sendEvent({
                name: 'clearMessageCountFriend'
            });
            getStorageAll();
            ajaxRequest('/User/smsCountReduce/type/1?userName='+user_name, 'post', {}, function (ret, err) {});
       }
</script>

</html>